#pragma once

// This file was generated.

#include "RNCWebViewComponentDescriptor.h"
#include "RNCWebViewJSIBinder.h"
#include "RNCWebViewModule.h"
#include "RNOH/Package.h"

namespace rnoh {

class BaseReactNativeWebviewPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
public:
    SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
        if (name == "RNCWebViewModule") {
            return std::make_shared<RNCWebViewModule>(ctx, name);
        }
        return nullptr;
    };
};

class BaseReactNativeWebviewPackageEventEmitRequestHandler : public EventEmitRequestHandler {
public:
    void handleEvent(Context const &ctx) override {
        auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
        if (eventEmitter == nullptr) {
            return;
        }

        std::vector<std::string> supportedEventNames = {
            "contentSizeChange",
            "renderProcessGone",
            "contentProcessDidTerminate",
            "customMenuSelection",
            "fileDownload",
            "loadingError",
            "loadingFinish",
            "loadingProgress",
            "loadingStart",
            "httpError",
            "message",
            "openWindow",
            "scroll",
            "shouldStartLoadWithRequest",
        };
        if (std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) !=
            supportedEventNames.end()) {
            eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
        }
    }
};


class RNCWebViewPackage : public Package {
public:
    RNCWebViewPackage(Package::Context ctx) : Package(ctx){};

    std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
        return std::make_unique<BaseReactNativeWebviewPackageTurboModuleFactoryDelegate>();
    }

    std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
        return {
            facebook::react::concreteComponentDescriptorProvider<facebook::react::RNCWebViewComponentDescriptor>(),
        };
    }

    ComponentJSIBinderByString createComponentJSIBinderByName() override {
        return {
            {"RNCWebView", std::make_shared<RNCWebViewJSIBinder>()},
        };
    };

    EventEmitRequestHandlers createEventEmitRequestHandlers() override {
        return {
            std::make_shared<BaseReactNativeWebviewPackageEventEmitRequestHandler>(),
        };
    }
};

} // namespace rnoh
